#pragma once

#include <iostream>
#include <sys/types.h>
#include <sys/stat.h>
#include <sstream>
#include <fcntl.h>
#include <unistd.h>
#include "Log.hpp"

class Util
{
public:
    static bool ReadFile(const std::string &Path, std::string *outstr)
    {
        struct stat st;//衡量文件内容的大小
        int n = stat(Path.c_str(),&st);
        if(n < 0)
            return false;
        int size = st.st_size;
        outstr->resize(size);
        int fd = open(Path.c_str(),O_RDONLY);
        if(fd < 0)
            return false;
        read(fd,(char*)outstr->c_str(),size);
        close(fd);
        LogMessage(Debug,"read file success");
        return true;
    }

    static std::string ReadOneLine(std::string& message,const std::string& SEP)
    {
        auto pos = message.find(SEP);
        if(pos == std::string::npos)
            return "";
        std::string res = message.substr(0,pos);
        message.erase(0,pos+SEP.size());
        return res;
    }

    static bool ParseLine(std::string& line,std::string* path,std::string* url,std::string* version)
    {
        std::stringstream ss(line);
        ss >> *path >> *url >> *version;
        return true;
    }
};
